/*
 * InversePoland.c
 *
 *  Created on: 2013-5-2
 *      Author: xkaying
 */

#include "InversePoland.h"
#include "stack.h"
#include "list.h"

#include <assert.h>
#include <stdlib.h>
//实现逆波兰表达式

Stack* InversePoland(List *ptokenlist, InversePolandEach each)
{
	assert(ptokenlist); //传入的表达式列表
	assert(each); //单次处理函数
	Token *ptoken;
	//临时存储栈
	Stack *pstack;
	pstack = StackInit(0);
	Stack *ptokenstack;
	ptokenstack = StackInit(0);
	ptoken = ListExport(ptokenlist);

	while (ptoken) {
		if (!each(ptokenstack, ptoken, pstack)) {
			free(ptoken);
			free(pstack);
			free(ptokenstack);
			return 0;
		}
		ptoken = (Token*) ListExport(ptokenlist);
	}
	//将pstack中数据全部导入到ptokenstack中
	ptoken=StackPop(pstack);
	while(ptoken){
		StackPush(ptokenstack,ptoken);
		ptoken=StackPop(pstack);
	}
	free(pstack);
	return ptokenstack;

}
